# Maintainer: Andreas 'Segaja' Schleifer <segaja at archlinux dot org>

pkgname="rubocop"
pkgver=1.39.0
pkgrel=2
pkgdesc='A Ruby code style checking and code formatting tool'
arch=('any')
url='https://rubocop.org/'
license=('MIT')
provides=('ruby-rubocop')
depends=('ruby' 'ruby-memory_profiler' 'ruby-parallel' 'ruby-parser' 'ruby-rainbow' 'ruby-regexp_parser' 'ruby-rubocop-ast' 'ruby-ruby-progressbar' 'ruby-unicode-display_width')
checkdepends=('asciidoctor' 'procps-ng' 'ruby-bump' 'ruby-rspec' 'ruby-rubocop-performance' 'ruby-rubocop-rake' 'ruby-rubocop-rspec' 'ruby-simplecov' 'ruby-stackprof' 'ruby-test-queue' 'ruby-webmock' 'ruby-yard')
options=('!emptydirs')
source=("https://github.com/rubocop/rubocop/archive/v${pkgver}/${pkgname}-${pkgver}.tar.gz"
  "${pkgname}_fix_changelog_duplication.patch::https://github.com/rubocop/rubocop/commit/60dbfdb8897c74726a2d9336c1f812dff3a94ea4.patch")
sha512sums=('0ad3d117907a327035dfbccdc220828d1ab543016c27b837b39a63faa036208c797d26903eb926ff97c7d05838e1f0f530c8f6bd57c807a2911452d1a565f102'
            '51a811fc31ab1a11de4eb8cd840e67ff879af4973354b5c1bc67ec1d42804de8def426a6906928900b1aea70b15a86dc66ff134ee548a6833e6b4f601782fd3e')
b2sums=('7a27313959227ab5a74ef42625872daadd95a09b12bb1d080e59b4ae92663d73c0fbd03b067c2c2e77268849221484b653840f7df0f9bf7f7358bea025b11ae0'
        '07e417229618ec59721622e0f84b8605bcdd39f3de225f22a4272fff4897e9a9702b1b254935f1583dc1eb46ac7816760bd3fa7a043b163f0b52a77b4b12026f')

prepare() {
  cd "${pkgname}-${pkgver}"

  patch --verbose --strip=1 --input="../${pkgname}_fix_changelog_duplication.patch"

  # update gemspec/Gemfile to allow newer version of the dependencies
  sed --in-place --regexp-extended 's|~>|>=|g' "${pkgname}.gemspec" Gemfile

  # don't force an upper bound on simplecov
  sed --in-place --regexp-extended "s|gem 'simplecov'.*|gem 'simplecov'|" Gemfile

  # we need webmock
  sed --in-place '/group/d' Gemfile
  sed --in-place '/end/d' Gemfile
}

build() {
  cd "${pkgname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  gem build "${pkgname}.gemspec"

  gem install \
    --local \
    --verbose \
    --ignore-dependencies \
    --no-user-install \
    --install-dir "tmp_install/${_gemdir}" \
    --bindir "tmp_install/usr/bin" \
    "${pkgname}-${pkgver}.gem"

  # remove unrepreducible files
  rm --force --recursive --verbose \
    "tmp_install/${_gemdir}/cache/" \
    "tmp_install/${_gemdir}/gems/${pkgname}-${pkgver}/vendor/" \
    "tmp_install/${_gemdir}/doc/${pkgname}-${pkgver}/ri/ext/"

  find "tmp_install/${_gemdir}/gems/" \
    -type f \
    \( \
      -iname "*.o" -o \
      -iname "*.c" -o \
      -iname "*.so" -o \
      -iname "*.time" -o \
      -iname "gem.build_complete" -o \
      -iname "Makefile" \
    \) \
    -delete

  find "tmp_install/${_gemdir}/extensions/" \
    -type f \
    \( \
      -iname "mkmf.log" -o \
      -iname "gem_make.out" \
    \) \
    -delete
}

check() {
  cd "${pkgname}-${pkgver}"

  local _gemdir="$(gem env gemdir)"

  GEM_HOME="tmp_install/${_gemdir}" rake ascii_spec spec
}

package() {
  cd "${pkgname}-${pkgver}"

  cp --archive --verbose tmp_install/* "${pkgdir}"

  install --verbose -D --mode=0644 LICENSE.txt "${pkgdir}/usr/share/licenses/${pkgname}/LICENSE"
  install --verbose -D --mode=0644 *.md --target-directory "${pkgdir}/usr/share/doc/${pkgname}"
}

# vim: tabstop=2 shiftwidth=2 expandtab:
